package com.ningxun.rms.web.controller.wechatrequire;
import com.ningxun.rms.common.core.controller.BaseController;
import com.ningxun.rms.common.core.page.TableDataInfo;
import com.ningxun.rms.common.utils.bean.BeanUtils;
import com.ningxun.rms.domain.vo.WechatRequireDetailVo;
import com.ningxun.rms.domain.vo.WechatRequireListVo;
import com.ningxun.rms.domain.vo.WechatRequireVo;
import com.ningxun.rms.domain.busrequire.BusRequire;
import com.ningxun.rms.service.baserequire.IBusRequireService;import com.ningxun.rms.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import com.ningxun.rms.common.core.domain.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/wechat/require")
public class WechatRequireController extends BaseController {
    @Autowired
    private IBusRequireService busRequireService;

    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查看岗位详情
     */
    @GetMapping("/{requireId}")
    public AjaxResult getInfoByRequireId(@PathVariable("requireId") Long requireId) {
        BusRequire item = busRequireService.selectFrontBusRequireByRequireId(requireId);
        //校验是否有效
        if(item.getRequireState()==1L) return AjaxResult.error("打开失败：无效的内容");
        WechatRequireDetailVo vo = WechatRequireDetailVo.convertToWechatRequireVo(item);
        return AjaxResult.success(Collections.singletonList(vo));
    }

/*    @RequestMapping("/select")
    public AjaxResult selectInfo(String selectString, Long deptId) {
         //deptId=100时，说明是全部地市，查全部
        Long realDeptId = (deptId != null && deptId == 100L) ? null : deptId;
        List<BusRequire> items = busRequireService.selectInfo(selectString, realDeptId);
        if (items == null || items.isEmpty()) return AjaxResult.error("未找到数据");
        return busRequireService.setListEntity(items);
        }*/
    /**
     * 分页查询岗位需求
     */
    @RequestMapping("/pages")
    public TableDataInfo selectInfoWithPage(String searchQuery, Long deptId,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        List<Long> deptIds = new ArrayList<>();
        if (deptId != null) {
            if (deptId == 100) {
                // 查询全部地市（ID=100）及其所有子部门
                deptIds = sysDeptService.selectDeptIdsByParentId(100L);
                deptIds.add(100L); // 包含顶级部门自身
            } else {
                // 查询指定地市及其子部门
                deptIds = sysDeptService.selectDeptIdsByParentId(deptId);
                deptIds.add(deptId); // 包含自身
            }
        }

        PageHelper.startPage(pageNum, pageSize);
        List<BusRequire> items = busRequireService.selectInfo(searchQuery,deptIds);
        PageInfo<BusRequire> pageInfo = new PageInfo<>(items);

        // 用service层的转换方法
        List<WechatRequireListVo> voList = WechatRequireListVo.setListEntity(items);
        TableDataInfo data = getDataTable(voList);
        data.setPages(pageInfo.getPages());
        data.setTotal(pageInfo.getTotal());
        return data;
    }
}
